<template>
  <div class="login-wrap">
    <div class="login">
      <el-form
        ref="pwdFormRef"
        :model="pwdForm"
        :rules="pwdRules"
        class="pwd-form"
      >
        <!-- <h3 class="title">{{ title }}</h3> -->

        <h3 class="title">{{ $t("pwd.editPassword") }}</h3>
        <el-form-item prop="password">
          <el-input
            v-model="pwdForm.password"
            type="password"
            size="default"
            auto-complete="off"
            :placeholder="$t('login.password')"
            @keyup.enter="handlePwd"
          >
            <template #prefix>
              <svg-icon name="password" />
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input
            v-model="pwdForm.confirmPassword"
            type="password"
            size="default"
            auto-complete="off"
            :placeholder="$t('user.confirmPwd')"
            @keyup.enter="handlePwd"
          >
            <template #prefix>
              <svg-icon name="password" />
            </template>
          </el-input>
        </el-form-item>

        <el-form-item style="width: 100%">
          <el-button
            :loading="loading"
            type="primary"
            size="default"
            round
            style="width: 100%"
            @click.prevent="handlePwd"
          >
            <span v-if="!loading">{{ $t("login.submit") }}</span>
            <span v-else>{{ $t("login.submitting") }}...</span>
          </el-button>
        </el-form-item>
        <div style="text-align: center">
          <router-link class="link-type" :to="'/login'">{{
            $t("pwd.return")
          }}</router-link>
        </div>
      </el-form>
    </div>

    <!--  底部  -->
    <div class="el-pwd-footer">
      <div v-html="copyRight"></div>
    </div>
  </div>
</template>

<script setup name="pwd">
import { eidtPwd } from "@/api/system/login";
import LangSelect from "@/components/LangSelect/index.vue";
import defaultSettings from "@/settings";
import { ElMessageBox } from "element-plus";

const { proxy } = getCurrentInstance();
const router = useRouter();
const route = useRoute();
const codeUrl = ref("");
const pwdForm = reactive({
  password: "",
  confirmPassword: "",
  token: "",
  mid: "",
});

const pwdFormRef = ref(null);
const loading = ref(false);
const equalToPassword = (rule, value, callback) => {
  if (pwdForm.password !== value) {
    callback(new Error("两次输入的密码不一致"));
  } else {
    callback();
  }
};
const pwdRules = reactive({
  password: [
    { required: true, trigger: "blur", message: "请输入您的密码" },
    {
      validator: (rule, value, callback) => {
        const regex =
          /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*(),.?":{}|<>])[A-Za-z\d!@#$%^&*(),.?":{}|<>]{8,16}$/;
        if (!regex.test(value)) {
          callback(new Error("用户密码由8-16位大小写字母、数字、符号组合"));
        } else {
          callback();
        }
      },
      trigger: "blur",
    },
  ],
  confirmPassword: [
    { required: true, trigger: "blur", message: "请再次输入您的密码" },
    { required: true, validator: equalToPassword, trigger: "blur" },
  ],
});
const copyRight = computed(() => {
  return defaultSettings.copyright;
});

async function handlePwd() {
  proxy.$refs["pwdFormRef"].validate((valid) => {
    if (valid) {
      loading.value = true;
      eidtPwd(pwdForm)
        .then((res) => {
          if (res.code == 200) {
            ElMessageBox.alert(
              "<font color='red'>恭喜你，密码修改成功！</font>",
              "系统提示",
              {
                dangerouslyUseHTMLString: true,
                type: "success",
              }
            )
              .then(() => {
                router.push("/login");
              })
              .catch(() => {});
          }
        })
        .catch(() => {
          loading.value = false;
        });
    }
  });
}
function init() {
  const token = route.query?.token;
  const mid = route.query?.mid;
  if (token) {
    pwdForm.token = String(token);
  } else {
    router.push("/login");
  }
  if (mid) {
    pwdForm.mid = String(mid);
  } else {
    router.push("/login");
  }
}
init();
</script>

<style rel="stylesheet/scss" lang="scss">
@use "@/assets/styles/login.scss";
.pwd {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-size: cover;
  flex-direction: column;
  background: radial-gradient(
    220% 105% at top center,
    #1b2947 10%,
    #4b76a7 40%,
    #81acae 65%,
    #f7f7b6
  );
}
.pwd-form {
  padding: 15px 25px 15px 25px;
  height: 250px;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  // color: #fff;
}

.el-pwd-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #999999;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
</style>
